use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // 先添加新的整数类型status列
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .add_column(
                        ColumnDef::new(SpiderTask::NewStatus)
                            .integer()
                            .default(0)
                            .not_null()
                    )
                    .to_owned(),
            )
            .await?;

        // 更新新列的值
        let sql = format!(
            r#"UPDATE spider_task SET new_status = 
                CASE status 
                    WHEN '待运行' THEN 0 
                    WHEN '运行中' THEN 1 
                    WHEN '已完成' THEN 2 
                    ELSE 0 
                END;
            "#
        );
        manager.get_connection().execute_unprepared(&sql).await?;

        // 删除旧的status列
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .drop_column(SpiderTask::Status)
                    .to_owned(),
            )
            .await?;

        // 重命名新列为status
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .rename_column(SpiderTask::NewStatus, SpiderTask::Status)
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // 先添加新的字符串类型status列
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .add_column(
                        ColumnDef::new(SpiderTask::NewStatus)
                            .string_len(32)
                            .default("待运行")
                            .not_null()
                    )
                    .to_owned(),
            )
            .await?;

        // 更新新列的值
        let sql = format!(
            r#"UPDATE spider_task SET new_status = 
                CASE status 
                    WHEN 0 THEN '待运行' 
                    WHEN 1 THEN '运行中' 
                    WHEN 2 THEN '已完成' 
                    ELSE '待运行' 
                END;
            "#
        );
        manager.get_connection().execute_unprepared(&sql).await?;

        // 删除旧的status列
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .drop_column(SpiderTask::Status)
                    .to_owned(),
            )
            .await?;

        // 重命名新列为status
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .rename_column(SpiderTask::NewStatus, SpiderTask::Status)
                    .to_owned(),
            )
            .await
    }
}

#[derive(DeriveIden)]
enum SpiderTask {
    Table,
    Status,
    NewStatus,
}